package com.sumainfor.pc.epc.service.impl;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.epc.dao.HoDecoratePiMapper;
import com.sumainfor.pc.epc.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.epc.service.HoDecoratePiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class HoDecoratePiServiceImpl implements HoDecoratePiService {
    private ReentrantLock lock = new ReentrantLock(false);//
    Logger log = LoggerFactory.getLogger(HoDecoratePiServiceImpl.class);// 日志
    @Autowired
    private HoDecoratePiMapper hoDecoratePiMapper;
    @Autowired
    private ZpcMktFeign zpcMktFeign;
    /**
     *服务管家新房验收列表待验收、已驳回、待上传图片
     */
    @Override
    public List<Map<String, Object>> getHoDecoratePiList(Map<String, Object> params) {
        //获取当前登录人的编号(租后主管复用服务管家新房验收栏目仅查看)
        if (ContextUtils.getUserId()!=null&&!ContextUtils.getUserId().equals("30")){
            params.put("SysUserId", ContextUtils.getUserId());
        }
        //查询管家验房列表
        List<Map<String,Object>> epcList=hoDecoratePiMapper.getHoDecoratePiList(params);
        //保存房源编号
        List<String> hIdList=new ArrayList<String>();
        for (Map<String,Object> map: epcList){
            if(map.get("HouId")!=null){
                hIdList.add(map.get("HouId").toString());
            }
        }
        params.put("hIdList",hIdList);
        //根据房源编号查询房源的信息
        JsonResult marketResut=zpcMktFeign.getHouIdInfoList(params);
        if (marketResut.get("errorCode").toString().equals("0")){
            List<Map<String,Object>> marketList=(List<Map<String,Object>>)marketResut.get("result");
            for (Map<String,Object> map1:epcList){
                if (map1.get("HouId")!=null){
                    for (Map<String,Object> map2:marketList){
                        if (map2.get("HouId")!=null){
                            if (map1.get("HouId").toString().equals(map2.get("HouId").toString())){
                                map1.putAll(map2);
                            }
                        }
                    }
                }

            }
        }
        return epcList;
    }
    /**
     *查询一个装修计划中未验房通过和其他费用未确认的数量
     */
    public JsonResult getHoDecorateCount(Map<String,Object> params){
        JsonResult result=new JsonResult();
        if (params.get("PlanId")==null){
            return result.putFailed("参数不正确");
        }
        Map<String,Object> map=hoDecoratePiMapper.getHoDecorateCount(params);
        if (map!=null ){
            result.put(map);
        }else{
            result.putFailed("服务器繁忙");
        }
        return result;
    }

    /**
     * 租赁顾问签约的时候查询房源的装修计划预计完成时间
     */
    @Override
    public JsonResult getHoDpiPredictTime(Map<String, Object> params) {
        JsonResult result=new JsonResult();
        if (params.get("HouId")==null){
            return result.putFailed("参数不正确");
        }
        Map<String,Object> map=hoDecoratePiMapper.getHoDpiPredictTime(params);
        if (map!=null ){
            result.put(map);
        }else{
            result.putFailed("服务器繁忙");
        }
        return result;
    }

    /**
     *服务管家新房验收列表待验收、已驳回、待上传图片列表总数
     */
    @Override
    public Integer getHoDecoratePiListCount(Map<String, Object> params) {
        //获取当前登录人的编号(租后主管复用服务管家新房验收栏目仅查看)
        if (ContextUtils.getUserId()!=null&&!ContextUtils.getUserId().equals("30")){
            params.put("SysUserId", ContextUtils.getUserId());
        }

        return hoDecoratePiMapper.getHoDecoratePiListCount(params);
    }
}
